﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using UBCon.Core.Models;

namespace UBCon.Ribbon.Models
{
    /// <summary>
    /// Represents a model class for UBCon.Ribbon.RibbonTabItem control.
    /// </summary>
    internal class RibbonTabItemModel:DraggableControlModel
    {
        #region Private Fields

        private double windowRelativePosX, windowRelativePosY;

        #endregion

        #region Properties
      
        /// <summary>
        /// Gets or sets X location of the control relative to window.
        /// </summary>
        internal double WindowRelativePosX 
        {
            get 
            {
                return windowRelativePosX;
            }
            set 
            {
                windowRelativePosX = value;

                RibbonTabItem tabItem = UIElement as RibbonTabItem;
                if (tabItem != null && 
                    tabItem.IsContextual && 
                    tabItem.ContextualTabItemsGroup != null) 
                {
                    RibbonWindow window = Window.GetWindow(tabItem) as RibbonWindow;
                    if (window != null && window.Theme == Core.Enums.WindowTheme.Classic) 
                    {
                        windowRelativePosX -= System.Windows.SystemParameters.ResizeFrameHorizontalBorderHeight;
                    }
                    ContextualTabItemsGroup group = tabItem.ContextualTabItemsGroup;

                    if (group.Items.IndexOf(tabItem) == 0&&group.Header!=null) 
                    {
                        Canvas.SetLeft(group.Header, windowRelativePosX - 1);
                    }
                    //if (tabItem.IsSelected)
                    //{
                    //    RibbonBar ribbonBar = ItemsControl.ItemsControlFromItemContainer(tabItem) as RibbonBar;
                    //    if (ribbonBar != null)
                    //    {
                    //        Point p = tabItem.TranslatePoint(RibbonHelper.emptyPoint, ribbonBar);
                    //        ribbonBar.UpdateHiddenRectangle(p.X, tabItem.RenderSize.Width);
                    //    }
                    //}
                }
            }
        }

        /// <summary>
        /// Gets or sets Y location of the control relative to window.
        /// </summary>
        internal double WindowRelativePosY 
        {
            get 
            {
                return windowRelativePosY;
            }
            set 
            {
                windowRelativePosY = value;
            }
        }

        internal void UpdateParentsWidth() 
        {
            RibbonTabItem tabItem = UIElement as RibbonTabItem;
            
            if (tabItem != null &&
                        tabItem.IsContextual &&
                        tabItem.ContextualTabItemsGroup != null)
            {
                ContextualTabItemsGroup group = tabItem.ContextualTabItemsGroup;
                if (group.Items.IndexOf(tabItem) == 0)
                {
                    double width = 0;
                    foreach (var item in group.Items)
                    {
                        width += (item as RibbonTabItem).DesiredSize.Width;
                    }

                    group.Header.SetValue(ContextualItemsHeader.WidthProperty, width);

                    group.Header.SetValue(ContextualItemsHeader.HeightProperty, UBCon.Ribbon.SystemParameters.CaptionHeight);

                    RibbonWindow window = RibbonHelper.GetWindow(group);
                    if (window != null)
                    {
                        window.UpdateTitleLocation(window);
                    }
                }
            }
        }
        #endregion
    }
}
